package com.shuyuntu.proposal.service;

import com.shuyuntu.proposal.dao.IDelegateDao;
import com.shuyuntu.proposal.dao.IPermissionDao;
import com.shuyuntu.proposal.dao.IRoleDao;
import com.shuyuntu.proposal.entity.Delegate;
import com.shuyuntu.proposal.entity.Permission;
import com.shuyuntu.proposal.entity.Role;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by limeng on 16/7/21.
 */
@Service
@Transactional
public class RoleService {
    @Resource
    IRoleDao roleDao;
    @Resource
    IDelegateDao delegateDao;
    @Resource
    IPermissionDao permissionDao;

    public IPermissionDao getPermissionDao() {
        return permissionDao;
    }

    public void setPermissionDao(IPermissionDao permissionDao) {
        this.permissionDao = permissionDao;
    }

    public IRoleDao getRoleDao() {
        return roleDao;
    }

    public void setRoleDao(IRoleDao roleDao) {
        this.roleDao = roleDao;
    }

    public IDelegateDao getDelegateDao() {
        return delegateDao;
    }

    public void setDelegateDao(IDelegateDao delegateDao) {
        this.delegateDao = delegateDao;
    }

    /*-------------------------------------*/

    /**
     * 插入一条新的角色信息
     * @param role
     * @return
     */
    public int insertNewRole(Role role) {
        try {
            roleDao.insert(role);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 获取角色列表
     * @return
     */
    public List<Role> getRoleList() {
        List<Role> list = roleDao.queryList(null);
        return list;
    }

    /**
     * 根据角色的id,获取属于这个角色的所有用户
     * @param id role的id
     * @return
     */
    public List<Delegate> getDelegateByRole(int id) {
        Criterion ct1 = Restrictions.eq("role.id", id);
        return delegateDao.queryList(ct1);
    }

    /**
     * 修改角色名字
     * @param roleId 角色Id
     * @param name 要修改的名字
     * @return
     */
    public int changeRoleName(int roleId, String name) {
        if (roleId == 0 || roleId == 1) {
            return 3;
        }

        if (name == null || name.equals("")) return 2;

        Role role = roleDao.queryById(roleId);
        if (role == null) {
            return 1;
        }
        role.setName(name);

        try {
            roleDao.update(role);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 根据id删除角色
     * @param id 角色的id
     * @return
     */
    public int deleteRole(int id) {
        if (id == 0 || id == 1) {
            return 3;
        }
        Role role = roleDao.queryById(id);
        if (role == null) {
            return 1;
        }
        List<Delegate> delegateList = delegateDao.queryList(
                Restrictions.eq("role.id", role.getId()));
        try {
            Role delegateRole = roleDao.queryById(1);
            for (Delegate delegate : delegateList) {
                delegate.setRole(delegateRole);
                delegateDao.update(delegate);
            }
            roleDao.delete(role);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 将制定用户的角色修改
     * @param roleId 目标角色id
     * @param userIdList 用户组
     * @param successCount 成功修改个数
     * @return
     */
    public int addUserToRole(int roleId, List<String> userIdList, StringBuffer successCount) {
        Role role = roleDao.queryById(roleId);
        if (role == null) {
            return 1;
        }
        int count = 0;
        for (String userId : userIdList) {
            Delegate delegate = delegateDao.queryById(userId);

            if (delegate == null) continue;

            delegate.setRole(role);

            try {
                delegateDao.update(delegate);
                count++;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        successCount.append(count);
        return 0;
    }

    public int changePermissions(int id, List<Integer> permissionList) {
        Role role = roleDao.queryById(id);
        if (role == null) {
            return 1;
        }

        role.getPermissions().clear();

        for (Integer permissionId : permissionList) {
            Permission permission = permissionDao.queryById(permissionId);
            if (permission == null) continue;

            role.getPermissions().add(permission);
        }
        try {
            roleDao.update(role);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
}
